package com.joysuch.wwyt.edu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joysuch.wwyt.edu.entity.EduCertificateNotify;
import com.joysuch.wwyt.edu.mapper.EduCertificateNotifyMapper;
import com.joysuch.wwyt.edu.service.EduCertificateNotifyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 证书提醒Service业务层处理
 *
 * @author eric
 * @date 2021-04-16
 */
@Service
public class EduCertificateNotifyServiceImpl extends ServiceImpl<EduCertificateNotifyMapper, EduCertificateNotify> implements EduCertificateNotifyService {

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveEduCertificateNotify(EduCertificateNotify notify) {
        notify.validate();

        //唯一性校验
        if (ObjectUtil.isNotNull(getByName(notify.getId(), notify.getTenentId(), notify.getOrgCode(), notify.getName()))) {
            throw new RuntimeException("已存在该名称");
        }

        saveOrUpdate(notify);
    }

    @Override
    public EduCertificateNotify getByName(Long id, Long tenantId, String orgCode, String name) {
        LambdaQueryWrapper<EduCertificateNotify> lqw = new LambdaQueryWrapper<>();
        lqw.ne(id != null, EduCertificateNotify::getId, id);
        lqw.eq(EduCertificateNotify::getTenentId, tenantId);
        lqw.like(EduCertificateNotify::getOrgCode, orgCode);
        lqw.eq(EduCertificateNotify::getName, name);
        return getOne(lqw);
    }

    @Override
    public EduCertificateNotify findByCertId(Long certId) {
        EduCertificateNotify notify = baseMapper.findByCertId(certId);
        return notify;
    }
}
